package org.glorylight.hacks.social.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.glorylight.hacks.social.data.NotificationData;
import org.glorylight.hacks.social.model.Notification;
import org.glorylight.hacks.social.util.ModelHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class NotificationService extends BaseService {
	private static final Logger log = LoggerFactory.getLogger(PictureService.class);

	@Transactional(readOnly = false)
	public void addNotification(NotificationData data) throws Exception {
		if (log.isDebugEnabled())
			log.debug("adding new notify for: {}", data.getNotifyUser());
		add(ModelHelper.convertNotificationData(data));
	}
	
	@Transactional(readOnly = false)
	public void deleteNotification(NotificationData data) throws Exception {
		if (log.isDebugEnabled())
			log.debug("delete notification for: {}", data.getId());
		Notification not = findNotification(data.getId());
		delete(not);
	}

	private Notification findNotification(UUID id) {
		if (log.isDebugEnabled())
			log.debug("retrieve notification: {}", id);
		return em.find(Notification.class, id);
	}
	
	public NotificationData find(UUID id) {
		if (log.isDebugEnabled())
			log.debug("retrieve notification: {}", id);
		Notification not = em.find(Notification.class, id);
		
		if (not != null)
			return ModelHelper.convertNotification(not);
		else
			return null;
	}

	public List<NotificationData> findAll() {
		if (log.isDebugEnabled())
			log.debug("find all Picture");
		List<Notification> result = em.createQuery("from Notification", Notification.class).getResultList();
		List<NotificationData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<NotificationData>();
			for (Notification u : result)
				datas.add(ModelHelper.convertNotification(u));
		}
		
		return datas;
	}
	
	public List<NotificationData> findAllByUser(String userId) {
		if (log.isDebugEnabled())
			log.debug("find all pictures");
		List<Notification> result = em.createNamedQuery("notification-by-user", Notification.class)
				.setParameter("userId", userId).getResultList();
		List<NotificationData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<NotificationData>();
			for (Notification u : result)
				datas.add(ModelHelper.convertNotification(u));
		}
		
		return datas;
	}
	
	public List<NotificationData> findAllByUserDate(String userId, Date startDate, Date endDate) {
		if (log.isDebugEnabled())
			log.debug("find all books by user date");
		List<Notification> result = em.createNamedQuery("notification-by-user-date", Notification.class)
				.setParameter("userId", userId)
				.setParameter("startDate", startDate)
				.setParameter("endDate", endDate).getResultList();
		List<NotificationData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<NotificationData>();
			for (Notification u : result)
				datas.add(ModelHelper.convertNotification(u));
		}
		
		return datas;
	}
	
	public List<NotificationData> findAllByDate(Date startDate, Date endDate) {
		if (log.isDebugEnabled())
			log.debug("find all pictures by date");
		List<Notification> result = em.createNamedQuery("notification-by-date", Notification.class)
				.setParameter("startDate", startDate)
				.setParameter("endDate", endDate).getResultList();
		List<NotificationData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<NotificationData>();
			for (Notification u : result)
				datas.add(ModelHelper.convertNotification(u));
		}
		
		return datas;
	}
}
